package groupby.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UtilsTools {

	/**
	 * 乱码处理
	 * @return
	 */
	public static String convertToChinese(String str)
	{
		String keyword=null;
		try {
			if(str!=null)
			{
			  keyword = new String(str.getBytes("ISO-8859-1"), "utf-8");
			}
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		return keyword;
	}
	/**
	 * 提取图片
	 * @param htmlStr
	 * @return
	 */
	public static List<String> getImgStr(String htmlStr){      
	     String img="";      
	     Pattern p_image;      
	     Matcher m_image;      
	     List<String> pics = new ArrayList<String>();   
	 
	    String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址      
	     p_image = Pattern.compile    
	             (regEx_img,Pattern.CASE_INSENSITIVE);      
	   m_image = p_image.matcher(htmlStr);    
	    while(m_image.find()){      
	        img = img + "," + m_image.group();      
	        Matcher m  = Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src   
	         while(m.find()){   
	            pics.add(m.group(1)); 
	            
	         }   
	     }      
	        return pics;      
	 }  
	/**
	 * 提取图片
	 * @param htmlStr
	 * @return
	 */
	public static String deleteImgStr(String content,int length,String str){      
		
		content = content.replaceAll("&nbsp;", "");
		content = content.replaceAll("\\<.*?>", "");
		if(content.length()>length)
		{
			content=content.substring(0,length)+str;
		}
		return content;  
	 } 
	

}
